
// g_prog1.cpp : Defines the class behaviors for the application.
//

#include "stdafx.h"
#include "g_prog1.h"
#include "g_prog1Dlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// Cg_prog1App

BEGIN_MESSAGE_MAP(Cg_prog1App, CWinAppEx)
	ON_COMMAND(ID_HELP, &CWinApp::OnHelp)
END_MESSAGE_MAP()


// Cg_prog1App construction

Cg_prog1App::Cg_prog1App()
{
	// TODO: add construction code here,
	// Place all significant initialization in InitInstance
}


// The one and only Cg_prog1App object

Cg_prog1App theApp;

int nice(int changepriority);

// Cg_prog1App initialization

BOOL Cg_prog1App::InitInstance()
{
	// InitCommonControlsEx() is required on Windows XP if an application
	// manifest specifies use of ComCtl32.dll version 6 or later to enable
	// visual styles.  Otherwise, any window creation will fail.
	INITCOMMONCONTROLSEX InitCtrls;
	InitCtrls.dwSize = sizeof(InitCtrls);
	// Set this to include all the common control classes you want to use
	// in your application.
	InitCtrls.dwICC = ICC_WIN95_CLASSES;
	InitCommonControlsEx(&InitCtrls);

	CWinAppEx::InitInstance();

	AfxEnableControlContainer();

	// Standard initialization
	// If you are not using these features and wish to reduce the size
	// of your final executable, you should remove from the following
	// the specific initialization routines you do not need
	// Change the registry key under which our settings are stored
	// TODO: You should modify this string to be something appropriate
	// such as the name of your company or organization
	SetRegistryKey(_T("Local AppWizard-Generated Applications"));

	try{
		nice(-2);
		Cg_prog1Dlg dlg;
		m_pMainWnd = &dlg;
		INT_PTR nResponse = dlg.DoModal();
		if (nResponse == IDOK)
		{
			// TODO: Place code here to handle when the dialog is
			//  dismissed with OK
		}
		else if (nResponse == IDCANCEL)
		{
			// TODO: Place code here to handle when the dialog is
			//  dismissed with Cancel
		}
	}
	catch(const CCustomException &e)
	{
		AfxMessageBox(e.GetErrorStr());
	}
	catch(...)
	{
		AfxMessageBox("Cg_prog1App::InitInstance unknown exception");
	}
	// Since the dialog has been closed, return FALSE so that we exit the
	//  application, rather than start the application's message pump.
	return FALSE;
}


int nice(int changepriority)
{
	HANDLE hProcess;
	DWORD curprior;
	int curpr = 0;

	hProcess = GetCurrentProcess();
	if(hProcess == 0) 
		throw CCustomException("nice: GetCurrentProcess failed");
	curprior = GetPriorityClass(hProcess);
	if(curprior == 0) 
		throw CCustomException("nice: GetPriorityClass failed");

	switch(curprior)
	{
	case IDLE_PRIORITY_CLASS:        curpr=0;   break;
	case NORMAL_PRIORITY_CLASS:      curpr=1;   break;
	case HIGH_PRIORITY_CLASS:        curpr=2;   break;
	case REALTIME_PRIORITY_CLASS:    curpr=2;   break;
	default: 
		//_ASSERT(0);
		curpr=0;
		break;
	}

	curpr += changepriority;

	if(curpr<0) curpr=0;
	if(curpr>2) curpr=2;

	switch(curpr)
	{
	case 0: curprior=IDLE_PRIORITY_CLASS;   break;
	case 1: curprior=NORMAL_PRIORITY_CLASS; break;
	case 2: curprior=HIGH_PRIORITY_CLASS;   break;
	default: break;
	}
	if(SetPriorityClass(hProcess,curprior)==0) 
		throw CCustomException("nice: SetPriorityClass failed");
	CloseHandle( hProcess);
	return 0;
}